import Vue from 'vue'
import store from '@/store'
import Router from 'vue-router'
import Home from '@/pages/home/Home.vue'
import Console from '@/pages/dashboard/Console'
import setting from '@/config/setting'

Vue.use(Router)

// 不需要权限的路由
const routes = [
    {
        path: '/',
        redirect: '/login'
    }, {
        path: '/dashboard',
        component: Home,
        meta: {
            title: '首页'
        },
        children: [
            {
                path: 'console',
                component: Console,
                meta: {
                    title: '首页'
                }
            }
        ]
    },
    {
        path: '/login',
        component: () => import('@/pages/login/Login'),
        meta: {
            title: '登录',
            newPage: true
        }
    }, {
        path: '/exception',
        component: Home,
        meta: {
            title: '异常页面'
        },
        children: [
            {
                path: '403',
                component: () => import('@/pages/exception/403'),
                meta: {
                    title: '403'
                }
            },
            {
                path: '404',
                component: () => import('@/pages/exception/404'),
                meta: {
                    title: '404'
                }
            },
            {
                path: '500',
                component: () => import('@/pages/exception/500'),
                meta: {
                    title: '500'
                }
            }
        ]
    }
]


// 需要权限的路由
export const allowRouters = [
    {
        path: '/',
        redirect: '/login'
    },

    {
        path: '/login',
        component: () => import('@/pages/login/Login'),
        meta: {
            title: '登录',
            newPage: true
        }
    },
    {
        path: '/dashboard',
        component: Home,
        meta: {
            title: '首页'
        },
        children: [
            {
                path: 'console',
                component: Console,
                meta: {
                    title: '首页'
                }
            }
        ]
    },
    {
        path: '/admin',
        component: Home,
        meta: {
            title: '权限管理'
        },
        children: [
            {
                path: 'admin',
                component: () => import('@/pages/admin/Admin'),
                meta: {
                    title: '管理员列表'
                }
            },
            {
                path: 'role',
                component: () => import('@/pages/admin/Role'),
                meta: {
                    title: '角色管理'
                }
            },
            {
                path: 'route',
                component: () => import('@/pages/admin/Route'),
                meta: {
                    title: '路由管理'
                }
            },
            {
                path: 'menu',
                component: () => import('@/pages/admin/Menu'),
                meta: {
                    title: '菜单管理'
                }
            },
            {
                path: 'auth',
                component: () => import('@/pages/admin/Auth'),
                meta: {
                    title: '权限分配'
                }
            },
        ]
    },
    {
        path: '/log',
        component: Home,
        meta: {
            title: '日志管理'
        },
        children: [
            {
                path: 'login-log',
                component: () => import('@/pages/log/LoginLog'),
                meta: {
                    title: '登录日志'
                }
            },
            {
                path: 'action-log',
                component: () => import('@/pages/log/ActionLog'),
                meta: {
                    title: '操作日志'
                }
            },
        ]
    },
    { 
        path: '/product', 
        component: Home, 
        meta: { 
            title: '产品管理', 
        }, 
        children: [ 
            { 
                path: 'product-type', 
                component: () => import('@/pages/product/ProductType'), 
                meta: { 
                        title: '商品类型管理' 
                } 
            }, 
            { 
                path: 'product', 
                component: () => import('@/pages/product/Product'), 
                meta: { 
                        title: '商品管理' 
                } 
            }, 
        ] 
    }, 
    { 
        path: '/order', 
        component: Home, 
        meta: { 
            title: '订单管理', 
        }, 
        children: [ 
            { 
                path: 'order-info', 
                component: () => import('@/pages/order/OrderInfo'), 
                meta: { 
                        title: '订单详情管理' 
                } 
            }, 
            { 
                path: 'order-cart', 
                component: () => import('@/pages/order/OrderCart'), 
                meta: { 
                        title: '购物车管理' 
                } 
            }, 
            { 
                path: 'order', 
                component: () => import('@/pages/order/Order'), 
                meta: { 
                        title: '订单管理' 
                } 
            }, 
        ] 
    }, 
    { 
        path: '/config', 
        component: Home, 
        meta: { 
            title: '配置管理', 
        }, 
        children: [ 
            { 
                path: 'config-notice', 
                component: () => import('@/pages/config/ConfigNotice'), 
                meta: { 
                        title: '公告管理' 
                } 
            }, 
            { 
                path: 'config-partner', 
                component: () => import('@/pages/config/ConfigPartner'), 
                meta: { 
                        title: '合伙人提成' 
                } 
            }, 
            { 
                path: 'config-level', 
                component: () => import('@/pages/config/ConfigLevel'), 
                meta: { 
                        title: '级别管理' 
                } 
            }, 
            { 
                path: 'config-carousel', 
                component: () => import('@/pages/config/ConfigCarousel'), 
                meta: { 
                        title: '轮播图管理' 
                } 
            }, 
            { 
                path: 'config-bank', 
                component: () => import('@/pages/config/ConfigBank'), 
                meta: { 
                        title: '银行管理' 
                } 
            }, 
            { 
                path: 'config-bank', 
                component: () => import('@/pages/config/ConfigBank'), 
                meta: { 
                        title: '银行管理' 
                } 
            }, 
        ] 
    }, 
    { 
        path: '/user', 
        component: Home, 
        meta: { 
            title: '用户管理', 
        }, 
        children: [ 
            { 
                path: 'user-price-record', 
                component: () => import('@/pages/user/UserPriceRecord'), 
                meta: { 
                        title: '余额变更记录' 
                } 
            }, 
            { 
                path: 'user-present', 
                component: () => import('@/pages/user/UserPresent'), 
                meta: { 
                        title: '提现管理' 
                } 
            }, 
            { 
                path: 'user-message', 
                component: () => import('@/pages/user/UserMessage'), 
                meta: { 
                        title: '消息管理' 
                } 
            }, 
            { 
                path: 'user-join-record', 
                component: () => import('@/pages/user/UserJoinRecord'), 
                meta: { 
                        title: '合伙人申请管理' 
                } 
            }, 
            { 
                path: 'user-client', 
                component: () => import('@/pages/user/UserClient'), 
                meta: { 
                        title: '客户管理' 
                } 
            }, 
            { 
                path: 'user-bank', 
                component: () => import('@/pages/user/UserBank'), 
                meta: { 
                        title: '用户银行卡' 
                } 
            }, 
            { 
                path: 'user', 
                component: () => import('@/pages/user/User'), 
                meta: { 
                        title: '用户管理' 
                } 
            }, 
        ] 
    }, 
    {
        path: '/sys',
        component: Home,
        meta: {
            title: '系统管理'
        },
        children: [
            {
                path: 'other-config',
                component: () => import('@/pages/sys/OtherConfig'),
                meta: {
                    title: '参数管理'
                }
            },
            {
                path: 'config',
                component: () => import('@/pages/sys/Config'),
                meta: {
                    title: '配置管理'
                }
            },
            {
                path: 'ip-config',
                component: () => import('@/pages/sys/IpConfig'),
                meta: {
                    title: 'IP管理'
                }
            },
        ]
    },
]


const router = new Router({
    routes: allowRouters
})

router.beforeEach((to, from, next) => {
    let isLogin = false
    let {meta, matched} = to
    let {title, newPage, keepAlive} = meta
    let sys = JSON.parse(localStorage.getItem("sys"))

    if (sys) {
        isLogin = sys.user.isLogin
    }

    to.params.keepAlive = keepAlive

    // if(!isLogin && to.path !== '/login') {
    //   next('/login')
    //   return
    // }else {
    let {menuList} = store.state.menu

    if (menuList.length > 0) { // 菜单数据加载成功
        if (!matched.length) {   // 打开的页面不存在
            router.push('/exception/404')
            return
        }
    }
    next()
    // }

    // 不是标签页
    if (newPage) {
        return
    }

    // 路由添加到标签页
    store.dispatch('worktab/worktabRoute', {
        to: {
            name: to.name ? to.name : '',
            title: (to.meta && title) ? title : '',
            path: to.path,
            params: to.params
        },
        from: {
            name: from.name ? from.name : '',
            title: (from.meta && from.meta.title) ? from.meta.title : '',
            path: from.path,
            params: to.params
        }
    })

    // 设置网页title
    if (title) {
        document.title = `${title} - ${setting.systemName}`
    }
    return
})

export default router
